package com.yc.chaoshi.cs_manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.chaoshi.cs_manage.DTO.AdminDTO;
import com.yc.chaoshi.cs_manage.mapper.UmsUserMapper;
import com.yc.chaoshi.cs_manage.service.IUmsAdminRolesService;
import com.yc.chaoshi.cs_manage.service.IUmsUserService;
import com.yc.chaoshi.entity.UmsAdminRoles;
import com.yc.chaoshi.entity.UmsResource;
import com.yc.chaoshi.entity.UmsUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xz
 * @since 2022-08-04
 */
@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements IUmsUserService {

    @Resource
    IUmsAdminRolesService adminRolesService;
    @Override
    public Page listAll(Integer pageNum, Integer pageSize, String sreachname, String sreachrole, Integer post) {
        Page<AdminDTO> page=new Page<>(pageNum,pageSize);
        pageNum=pageSize*(pageNum-1);
        List<AdminDTO> list=baseMapper.listAll(pageNum,pageSize,sreachname,sreachrole,post);
        long total=baseMapper.listAll(0,10000,sreachname,sreachrole,post).size();
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }

    @Override
    public boolean updatestatus(Integer id, Integer status) {
        UpdateWrapper<UmsUser> updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().eq(UmsUser::getId,id)
                              .set(UmsUser::getStatus,status);
        return this.update(updateWrapper);
    }

    @Override
    @Transactional
    public Boolean edit(AdminDTO adminDTO) {
        UmsUser umsUser=new UmsUser();
        BeanUtils.copyProperties(adminDTO,umsUser);
        UpdateWrapper<UmsAdminRoles> umsAdminRolesUpdateWrapper=new UpdateWrapper<>();
        umsAdminRolesUpdateWrapper.lambda().eq(UmsAdminRoles::getAdminId,umsUser.getId())
                                           .set(UmsAdminRoles::getRoleId,adminDTO.getRolename());

        if (this.updateById(umsUser)&& adminRolesService.update(umsAdminRolesUpdateWrapper)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional
    public Boolean add(AdminDTO adminDTO) {
        UmsUser umsUser=new UmsUser();
        BeanUtils.copyProperties(adminDTO,umsUser);
        umsUser.setCreateTime(LocalDateTime.now());
        umsUser.setStatus(1);
        umsUser.setPost(String.valueOf(1));
        int count=this.baseMapper.insertGid(umsUser);
        UmsAdminRoles umsAdminRoles=new UmsAdminRoles();
        umsAdminRoles.setAdminId(umsUser.getId());
        umsAdminRoles.setRoleId(Integer.valueOf(adminDTO.getRolename()));
        boolean result=adminRolesService.save(umsAdminRoles);
        if (count!=0&&result){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean user_add(AdminDTO adminDTO) {
        UmsUser umsUser=new UmsUser();
        BeanUtils.copyProperties(adminDTO,umsUser);
        umsUser.setCreateTime(LocalDateTime.now());
        umsUser.setStatus(1);
        umsUser.setPost(String.valueOf(0));
        int count=this.baseMapper.insertGid(umsUser);
        if (count!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean user_edit(AdminDTO adminDTO) {
        UmsUser umsUser=new UmsUser();
        BeanUtils.copyProperties(adminDTO,umsUser);
        if (this.updateById(umsUser)){
            return true;
        }else {
            return false;
        }
    }
    public List<UmsResource>  ResourceByid(Integer id) {
       return this.baseMapper.ResourceByid(id);
    }
}
